<template>
  <div>
    <el-dialog
      :title="typeName"
      :close-on-click-modal="false"
      append-to-body
      :visible.sync="visible"
      class="jsbos-dialog jsbos-dialog_center"
      lock-scroll
      width="600px"
    >
      <el-form
        :model="dataForm"
        :rules="rules"
        ref="dataForm"
        label-width="100px"
      >
        <el-form-item label="人员：" prop="executeUser">
          <RangeUser
            v-if="flag2"
            @change="actorChange"
            v-model="dataForm.executeUser"
            :rangeList="executeList"
            :multiple="false"
          />
        </el-form-item>
        <el-form-item label="动态描述：" prop="description">
          <el-input
            type="textarea"
            :rows="3"
            v-model="dataForm.description"
            placeholder="请输入"
            clearable
            :style="{ width: '100%' }"
            maxlength="25"
            show-word-limit
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="moveSalesSubmit()"
          :loading="submitLoad"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { assignOrder } from "@/views/questionnaire/api/complaint";
import { getConfigByKeyName } from "@/api/system/sysConfig";
export default {
  components: {},
  props: [],
  data() {
    return {
      typeName: "",
      flag2: true,
      executeList: [
        { type: "company", selectArr: [] },
        {
          type: "department",
          selectArr: [],
        },
        {
          type: "Role",
          selectArr: [],
        },
        {
          type: "Position",
          selectArr: [],
        },
        {
          type: "Group",
          selectArr: [],
        },
        {
          type: "user",
          selectArr: [],
        },
        {
          type: "exclude",
          selectArr: [],
        },
      ],
      type: "",
      optionsUser: [],
      enableMark: "",
      submitLoad: false,
      visible: false,
      dataForm: {
        id: "",
        executorUserId: "",
        description: "",
        objectName: "",
      },
      rules: {
        executorUserId: [
          {
            required: true,
            message: "指派人员不能为空",
            trigger: "change",
          },
        ],
        description: [
          {
            required: true,
            message: "动态描述不能为空",
            trigger: "change",
          },
        ],
      },
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    getExecuteList() {
      let that = this;
      var cleanManageAuto = "complainManageAccept";
      getConfigByKeyName(cleanManageAuto)
        .then((res) => {
          this.flag2 = false;
          this.executeList = JSON.parse(res.data.keyValue);
          setTimeout(function () {
            that.flag2 = true;
          }, 0);
        })
        .catch(() => {});
    },
    actorChange(arr) {
      this.dataForm.description = "";
      var userName = this.$store.getters.userInfo.userName;
      var whetherdesc = this.type == "transfer" ? "转派" : "分派";

      var input =
        userName +
        whetherdesc +
        "投诉任务给" +
        arr[0].fullName.split("/")[0] +
        "。";
      this.dataForm.objectName = arr[0].fullName.split("/")[0];
      this.dataForm.description.includes(input)
        ? (this.dataForm.description = this.dataForm.description.replace(
            input,
            ""
          ))
        : (this.dataForm.description += input);
    },
    moveSalesSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.submitLoad = true;
          if (
            this.dataForm.description[this.dataForm.description.length - 1] ==
            "，"
          ) {
            this.dataForm.description = this.dataForm.description.substr(
              0,
              this.dataForm.description.length - 1
            );
          }

          assignOrder(this.dataForm)
            .then((res) => {
              this.submitLoad = false;
              this.visible = false;

              this.$emit("close", true);
              this.$message({
                message: this.type == "transfer" ? "转派成功！" : "分派成功！",
                type: "success",
              });
            })
            .catch((res) => {
              this.submitLoad = false;
            });
        }
      });
    },
    init(id, enableMark, type) {
      this.visible = true;
      this.getExecuteList();
      this.$nextTick(() => {
        this.enableMark = enableMark;
        this.type = type;
        this.typeName = this.type == "transfer" ? "转派" : "分派";
        this.$refs.dataForm.resetFields();
        this.dataForm.id = id || 0;
      });
    },
  },
};
</script>
